package com.xuebank.nfe.nfecar.beas

import android.annotation.TargetApi
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.os.SystemClock
import android.support.annotation.ColorInt
import android.support.annotation.ColorRes
import android.support.annotation.IdRes
import android.support.annotation.LayoutRes
import android.support.v4.content.ContextCompat
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.AdapterView
import android.widget.FrameLayout
import android.widget.Toast
import com.trello.rxlifecycle.components.support.RxAppCompatActivity
import com.wzn.libaray.utils.Logger
import com.wzn.libaray.utils.StatusBarUtil
import com.xuebank.nfe.nfecar.R
import com.xuebank.nfe.nfecar.utils.loading.ProgressDailog
import com.xuebank.nfe.nfecar.utils.loading.PromptView
import rx.Observable
import rx.android.schedulers.AndroidSchedulers

/**
 * Created by Wind_Fantasy on 15/3/26.
 */
abstract class BaseActivity : RxAppCompatActivity(), View.OnClickListener, AdapterView.OnItemClickListener, IBaseView {

    val TAG = javaClass.simpleName

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Logger.d(TAG, TAG + " onCreate")
        initData(savedInstanceState)
    }

    override fun onResume() {
        super.onResume()
        Logger.d(TAG, TAG + "  onResume")
    }

    protected fun onResume(onlyCallSuper: Boolean) {
        if (onlyCallSuper)
            super.onResume()
        else
            onResume()
    }

    override fun onPause() {
        super.onPause()
        Logger.d(TAG, TAG + "  onPause")
    }

    protected fun onPause(onlyCallSuper: Boolean) {
        if (onlyCallSuper)
            super.onPause()
        else
            onPause()
    }

    protected fun onStop(onlyCallSuper: Boolean) {
        if (onlyCallSuper)
            super.onStop()
        else
            onStop()
    }

    override fun onStop() {
        super.onStop()
        Logger.d(TAG, TAG + "  onStop")
    }

    override fun onDestroy() {
        cleanData()
        super.onDestroy()
        Logger.d(TAG, TAG + "  onDestroy")
    }

    protected fun onDestroy(onlyCallSuper: Boolean) {
        if (onlyCallSuper)
            super.onDestroy()
        else
            onDestroy()
    }



    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        mPromptView?.parent?.let {
            (it as ViewGroup).removeView(mPromptView)
        }

        getContentRootView()?.addView(mPromptView)
    }


    protected open fun initData(savedInstanceState: Bundle?) {}

    protected open fun initViews() {}

    protected open fun findViews() {}


    private val mPromptView: PromptView? by lazy {
        PromptView(getContext())
    }
    private var mProgressDailog: ProgressDailog? = null

    fun <T> loadingDialogManager(failedAction: (() -> Unit)? = null): Observable.Transformer<T, T> {
        return Observable.Transformer<T, T> { observable ->
            observable.doOnSubscribe { showProgressDialog() }.subscribeOn(AndroidSchedulers.mainThread())
                    .doOnUnsubscribe { dismissProgressDialog() }.doOnCompleted { dismissProgressDialog() }
                    .doOnError {
                        dismissProgressDialog()
                        failedAction?.let { getPromptView()?.onLoadingFailed(failedAction) }
                    }
        }
    }

    /**
     * @see StatusBarUtil.setColor
     */
    @TargetApi(19)
    @Deprecated("")
    fun setStatusBarTintColorResource(@ColorRes colorId: Int) {
        setStatusBarTintColor(ContextCompat.getColor(this, colorId))
    }

    /**
     * @see StatusBarUtil.setColor
     */
    @TargetApi(19)
    @Deprecated("")
    fun setStatusBarTintColor(@ColorInt color: Int) {
        StatusBarUtil.setColor(this, color)
    }

    private fun resetStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            StatusBarUtil.setDefaultColor(this)
        }
    }

    override fun setContentView(@LayoutRes layoutResID: Int) {
        super.setContentView(layoutResID)
        resetStatusBar()
    }

    override fun setTheme(resid: Int) {
        var rId = resid
        super.setTheme(rId)
    }

    override fun setContentView(view: View) {
        super.setContentView(view)
        resetStatusBar()
    }

    override fun setContentView(view: View, params: ViewGroup.LayoutParams) {
        super.setContentView(view, params)
        resetStatusBar()
    }


    override fun onContentChanged() {
        super.onContentChanged()
        findViews()
        initViews()
    }


    override fun onPostResume() {
        super.onPostResume()
        Logger.d(TAG, TAG + "  onPostResume")
    }

    protected fun onPostResume(onlyCallSuper: Boolean) {
        if (onlyCallSuper)
            super.onPostResume()
        else
            onPostResume()
    }

    protected fun <T : View> myFindViewById(@IdRes id: Int): T = findViewById<T>(id)


    /**
     * 在onDestroy中被回调
     */
    open fun cleanData() {

    }

    protected fun showToast(msg: String) {
        Toast.makeText(applicationContext, msg, Toast.LENGTH_SHORT).show()
    }

    protected fun showToast(resId: Int) {
        Toast.makeText(applicationContext, resId, Toast.LENGTH_SHORT).show()
    }


    /**
     * 是否可以继续执行

     * @return
     */
    override fun canGoon(): Boolean {
        return !isFinishing
    }

    override fun getContext(): BaseActivity {
        return this
    }

    // variable to track event time
    private var mLastClickTime: Long = 0

    //View.OnClickListener.onClick method defination

    override fun onClick(v: View) {
        // Preventing multiple clicks, using threshold of 1 second
        if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {
//            onViewDoubleClick(v)
            return
        }

        mLastClickTime = SystemClock.elapsedRealtime()

        onViewClick(v)
    }

    open fun onViewClick(v: View) {

    }

    open fun onViewDoubleClick(v: View) {

    }

    override fun onItemClick(parent: AdapterView<*>, view: View, position: Int, id: Long) {
        // Preventing multiple clicks, using threshold of 1 second
        if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {
            return
        }

        mLastClickTime = SystemClock.elapsedRealtime()

        onItemViewClick(parent, view, position, id)
    }

    fun onItemViewClick(parent: AdapterView<*>, view: View, position: Int, id: Long) {}

    override fun getPromptView(): PromptView? = mPromptView

    override fun getProgressDialog(): ProgressDailog? = mProgressDailog

    override fun showProgressDialog() {
        showProgressDialog(0)
    }

    override fun showProgressDialog(textId: Int) {
        var mutableTextId: Int = textId
        if (mutableTextId == 0) mutableTextId = R.string.common_pls_wait
        if (mProgressDailog == null) {
            mProgressDailog = ProgressDailog(this)
        }
        mProgressDailog?.setCanceledOnTouchOutside(false)
        mProgressDailog?.setContent(getString(mutableTextId))
        mProgressDailog?.show()
    }

    override fun showProgressDialog(msg: String?) {
        if (mProgressDailog == null) {
            mProgressDailog = ProgressDailog(this)
        }
        mProgressDailog?.setCanceledOnTouchOutside(false)
        mProgressDailog?.setContent(msg)
        mProgressDailog?.show()
    }

    override fun dismissProgressDialog() {
        if (mProgressDailog != null) {
            mProgressDailog?.dismiss()
        }
    }

    open fun getContentRootView(): FrameLayout? = null

    fun getParentView(): FrameLayout? {
        val rootView = window.decorView.findViewById<FrameLayout>(android.R.id.content)
        return rootView

    }

    fun hideSystemSoftKeyboard(view: View) {
        val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(view.windowToken, 0)
    }


}
